package org.accp.daner.web;

import entity.address;
import entity.reported_info;
import entity.sys_user_info;
import entity.warranty_info;
import org.accp.daner.dto.Dto;
import org.accp.daner.dto.PageBean;
import org.accp.daner.feignservice.baodan;
import org.accp.daner.feignservice.jurisdictionService;
import org.accp.daner.service.ReportedInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@CrossOrigin(methods= {RequestMethod.POST, RequestMethod.GET})
@RestController
public class ReportedInfoWeb {

    @Autowired
    private ReportedInfoService reported_infoService;
    @Autowired
    private baodan baodan;
    @Autowired
    private jurisdictionService jurisdiction;

    @RequestMapping("/list")
    public PageBean list(Dto dto){
        PageBean dto1=this.reported_infoService.queryAllByLimit(dto);
        return dto1;
    }
    @RequestMapping("/Byid")
    public reported_info queryByid(String id){
        reported_info r=this.reported_infoService.queryByid(id);
        return r;
    }
    @RequestMapping("/add")
    public Integer add(Dto dto) throws ParseException {
        //获取数据
        reported_info reported_info1=dto.getList();
        //获取车牌号
        System.out.println(dto.getShen());
        warranty_info w= this.baodan.chepaiselect(dto.getShen());
        if(w!=null) {
            //获取保单起止时间
            String time=w.getInsurance_begin_date();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            Date currdate = format.parse(time);
            Calendar ca = Calendar.getInstance();
            ca.setTime(currdate);
            //加7天
            ca.add(Calendar.DATE, 7);
            currdate = ca.getTime();
            Date date=new Date();
            //判断出险时间不在保险起止期7日内
            if (currdate.getTime()>date.getTime()) {
                reported_info1.setHandle("注销");
            }
            ca.setTime(date);
            //减3天
            ca.add(Calendar.DATE,-3);
            //新建
            reported_info reported_info2=new reported_info();
            //把保单号放进去
            reported_info2.setRecognizee(w.getWarranty_number());
            Dto d=new Dto();
            d.setList(reported_info2);
            //查询当前保单号的出险信息
            PageBean dto1=this.reported_infoService.queryAllByLimit(d);
            //接受查询的数据
            List<reported_info> list=(List<reported_info>)dto1.getData();
            //如果不等于空
            if(list!=null) {
                int s = -1;
                for (reported_info r : list) {
                    Date da = format.parse(r.getReported_time());
                    //判断他三日内有没有报过案
                    long i = (da.getTime() - date.getTime()) / (24 * 60 * 60 * 1000);
                    if (i < 3) {
                        s = 1;
                    }
                }
                if (s == 1) {
                    reported_info1.setHandle("注销");
                }
            }
            reported_info1.setRecognizee(w.getWarranty_number());
            reported_info1.setRecognizee_name(w.getCar_owner_name());
        }else{
            reported_info1.setHandle("注销");
        }
        this.reported_infoService.insert(reported_info1);
        return 1;
    }
    @RequestMapping("/update")
    public Integer update(reported_info r){
            this.reported_infoService.update(r);
        return 1;
    }
    @RequestMapping("ByidCount")
    public Integer byidCount(String handle){
        int count =this.reported_infoService.ByidCount(handle);
        return count;
    }
    @RequestMapping("/alladdress")
    public List<address> alladdress(String fid){
        return this.reported_infoService.alladdress(fid);
    }
        @RequestMapping("queryByidAddress")
    public address queryByidAddress(String id){
        return this.reported_infoService.queryByidAddress(id);
    }

    @RequestMapping("/getSysUser.do")
    public List<sys_user_info> getUser(){
      List<sys_user_info> list=this.jurisdiction.getUser();
      return list;
    }
}
